import requests
class byhy_api:
    # 打印code、请求头、和消息体方法
    def printresponse(self,response):
        print('\n----------------HTTP response *begin-----------')
        print('code:')
        print(response.status_code)
        print('请求头：')
        for k, v in response.headers.items():
            print(f'{k}:{v}')
        print('请求体：')
        print(response.json())
        print('\n----------------HTTP response *end-----------')

    # 请求login接口
    def login(self,data):
        self.s = requests.session()
        response = self.s.post("http://127.0.0.1:8047/api/mgr/signin",
                                       data)

        self.printresponse(response)
        return response
    #列出客户
    def list_customers(self,pagesize=10,pagenum=1,keywords=''):
        response = self.s.get("http://127.0.0.1:8047/api/mgr/customers",
                                       params={
                                           'action': 'list_customer',
                                           'pagesize': pagesize,
                                           'pagenum':pagenum,
                                           'keywords':keywords
                                       })

        self.printresponse(response)
        
        return response

    # 列出药品
    def list_medicines(self, pagesize=10, pagenum=1, keywords=''):
        response = self.s.get("http://127.0.0.1:8047/api/mgr/medicines",
                              params={
                                  'action': 'list_medicine',
                                  'pagesize': pagesize,
                                  'pagenum': pagenum,
                                  'keywords': keywords
                              })

        self.printresponse(response)

        return response

    # 列出订单
    def list_orders(self, pagesize=10, pagenum=1, keywords=''):
        response = self.s.get("http://127.0.0.1:8047/api/mgr/orders",
                              params={
                                  'action': 'list_order',
                                  'pagesize': pagesize,
                                  'pagenum': pagenum,
                                  'keywords': keywords
                              })

        self.printresponse(response)

        return response
    #删除客户
    def del_customers(self,customers_id):
        response = self.s.delete("http://127.0.0.1:8047/api/mgr/customers",
                                       json={
                                           'action': 'del_customer',
                                           'id': customers_id
                                       })

        self.printresponse(response)
        return response
    #删除所有客户
    def del_customers_all(self):
        response = self.list_customers(100,1)
        response = response.json()['retlist']
        for customers in response:
            self.del_customers(customers['id'])

    #删除药品
    def del_medicines(self,medicines_id):
        response = self.s.delete("http://127.0.0.1:8047/api/mgr/medicines",
                                       json={
                                           'action': 'del_medicine',
                                           'id':medicines_id
                                       })

        self.printresponse(response)
        return response
    #删除所有药品
    def del_medicines_all(self):
        response = self.list_medicines(100,1)
        response = response.json()['retlist']
        for customers in response:
            self.del_medicines(customers['id'])
    #删除订单
    def del_orders(self,orders_id):
        response = self.s.delete("http://127.0.0.1:8047/api/mgr/orders",
                                       json={
                                           'action': 'delete_order',
                                           'id': orders_id
                                       })

        self.printresponse(response)
        return response
    #删除所有订单
    def del_orders_all(self):
        response = self.list_orders(100,1)
        response = response.json()["retlist"]
        for customers in response:
            self.del_orders(customers['id'])
    #添加一个客户
    def add_customers(self,name,phonenumber,address):
        response = self.s.post("http://127.0.0.1:8047/api/mgr/customers",
                                       json={
                                           "action": "add_customer",
                                           "data": {
                                               "name": name,
                                               "phonenumber": phonenumber,
                                               "address": address
                                           }
                                       })

        self.printresponse(response)
        return response
    def add_custmers_10(self):
        for i in range(10):
            self.s.post("http://127.0.0.1:8047/api/mgr/customers",
                        json={
                            "action": "add_customer",
                            "data": {
                                "name": f'南京中医院{i}',
                                "phonenumber": f'1233333331{i}',
                                "address": f'南京市地点{i}'
                            }
                        })

#修改客户信息
    def up_custmers(self, custmers_id, up_name, up_phonenumber, up_address):
        response = self.s.put("http://127.0.0.1:8047/api/mgr/customers",
                                       json={
                                           "action": "modify_customer",
                                           'id' : custmers_id,
                                           "newdata": {
                                               "name": up_name,
                                               "phonenumber": up_phonenumber,
                                               "address": up_address
                                           }
                                       })

        return response



apimgr = byhy_api()#实例化类∂